<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderId">
        <el-input v-model="queryParams.orderId" placeholder="请输入订单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="店铺名称" prop="sellerSignerFullname">
        <el-input v-model="queryParams.sellerSignerFullname" placeholder="请输入店铺名称" clearable
                  @keyup.enter.native="handleQuery"/>
        <!--        <el-select v-model="queryParams.sellerSignerFullname" placeholder="请选择店铺名称" clearable filterable>-->
        <!--          <el-option v-for="(dict,index) in storeList" :key="index"-->
        <!--                     :label="dict.label" :value="dict.value"/>-->
        <!--        </el-select>-->
      </el-form-item>
      <el-form-item label="时间" prop="dateTime">
        <el-date-picker
          v-model="dateTime"
          type="datetimerange"
          @change="changeTime"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderAmountList" border stripe
              @selection-change="handleSelectionChange">
      <el-table-column label="订单号" align="center" prop="orderId"></el-table-column>
      <el-table-column label="卖家账号" align="center" prop="sellerLoginId"></el-table-column>
      <el-table-column label="店铺名称" align="center" prop="sellerSignerFullname"></el-table-column>
      <el-table-column label="支付金额" align="center" prop="payAmount"></el-table-column>
      <el-table-column label="测评费用" align="center" prop="evaluationFee"></el-table-column>
      <el-table-column label="测评成本" align="center" prop="evaluationCost"></el-table-column>
      <el-table-column label="联盟佣金" align="center" prop="afflicateFee"></el-table-column>
      <el-table-column label="交易佣金" align="center" prop="escrowFee"></el-table-column>
      <el-table-column label="预计可得" align="center" prop="expectAmount"></el-table-column>
      <el-table-column label="订单内部状态" align="center" prop="erpIinnerOrderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_inner_order_status" :value="scope.row.erpInnerOrderStatus"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="订单创建时间" align="center" prop="gmtCreate"></el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>

import {getEvaluationOrderList, getSellerStoreFullName} from "@/api/evaluation/evaluation";

export default {
  dicts: ['erp_inner_order_status'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      orderAmountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: undefined,
        startTime: undefined,
        endTime: undefined,
        sellerSignerFullname: undefined,
      },
      dateTime: undefined,
      // 表单参数
      form: {expenseType: "测评费用",},
      userList: [],// 卖家列表
      storeList: [],//店铺名称列表
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
    };
  },
  created() {
    if (this.$route.query.queryData) {
      this.queryData = JSON.parse(this.$route.query.queryData)
      // const mergedObj = {...this.queryParams, ...this.queryData}
      if (this.queryData.searchTime) {
        let time = this.queryData.searchTime
        let dateList = time.split('-')
        // console.log('dateList', dateList)
        let days = new Date(dateList[0], dateList[1] + 1, 0).getDate()
        this.dateTime = [time + '-' + '01 00:00:00', time + '-' + days + ' 23:59:59']
        this.queryParams.startTime = time + '-' + '01 00:00:00'
        this.queryParams.endTime = time + '-' + days + ' 23:59:59'
        this.queryParams.sellerLoginId = this.queryData.sellerLoginId
        // console.log('dataa', days)
      }
      // this.queryParams = mergedObj
      // console.log(' this.queryParams', this.queryParams)
    }
    this.getList();
    this.getStoreList()
  },
  methods: {
    // 获取店铺账号选择列表
    getStoreList() {
      this.loading = true;
      getSellerStoreFullName().then(res => {
        this.storeList = res.data;
        this.loading = false;
      });
    },
    /** 查询测评订单列表 */
    getList() {
      this.loading = true;
      getEvaluationOrderList(this.queryParams).then(res => {
        this.orderAmountList = res.rows
        this.loading = false;
        this.total = res.total
      });
    },
    // 选择时间
    changeTime(e) {
      this.queryParams.startTime = this.parseTime(this.dateTime[0])
      this.queryParams.endTime = this.parseTime(this.dateTime[1])
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        flowType: undefined,
        expenseType: "测评费用",
        payType: undefined,
        userId: undefined,
        money: undefined,
        orderNo: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.startTime = undefined
      this.queryParams.endTime = undefined
      this.queryParams.sellerLoginId = undefined
      this.dateTime = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增测评金额";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.expenseType = 14
          updateEvaluationAmount(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/purchase/sellerOrder/exportEvaluation', {
        ...this.queryParams
      }, `sellerStoreAccountLog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
